package com.auto.survey.activity.adjustment.adjustment_high

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.HighAddActivity.Companion.KEY_ADD_OR_UPDATE_PROJECT_ID
import com.auto.survey.base.ICreatView
import com.auto.survey.util.Helper
import kotlinx.coroutines.launch

class ProjectDiscribView(viewModel:HighAdjusMentModle, project_id:Int, activity: AppCompatActivity) :ICreatView {
    private lateinit var mView: View
    private val mActivity = activity
    private val mViewModel= viewModel
    private val mProjectId = project_id

    override fun creatView(context: Context, parent: ViewGroup) {
        mView = LayoutInflater.from(context).inflate(R.layout.view_project_discrib, parent, false)

        val tv_projectName = mView.findViewById<TextView>(R.id.tv_discrib_projname)
        val tv_projectTime = mView.findViewById<TextView>(R.id.tv_discrib_creatTime)
        val tv_projectPerson = mView.findViewById<TextView>(R.id.tv_discrib_person)

        val tv_point_total = mView.findViewById<TextView>(R.id.tv_point_total)
        val tv_observation_total = mView.findViewById<TextView>(R.id.tv_observation_total)
        val tv_knowp_total = mView.findViewById<TextView>(R.id.tv_knowp_total)
        val tv_unknowp_total = mView.findViewById<TextView>(R.id.tv_unknowp_total)

        val tv_proj_discrib = mView.findViewById<TextView>(R.id.tv_proj_discrib)

        mView.findViewById<TextView>(R.id.tv_discrib_proj_setting).setOnClickListener {
            val intent = Intent()
            intent.putExtra(KEY_ADD_OR_UPDATE_PROJECT_ID, mProjectId)
            intent.setClass(mActivity, HighAddActivity::class.java)
            mActivity.startActivity(intent)
        }

        mActivity.lifecycleScope.launch {
            mViewModel.project.observe(mActivity){
                tv_projectName.text = it?.project?.name
                tv_projectPerson.text = it?.project?.personName
                tv_projectTime.text =
                    it?.project?.createTime?.let { it1 -> Helper.formatTimestamp(it1, "yyyy-MM-dd hh:mm:ss") }
                tv_point_total.text = it?.project?.pointCount.toString()
                tv_observation_total.text = it?.project?.observationCount.toString()
                tv_proj_discrib.text = it?.project?.discrib
                tv_knowp_total.text = it?.knownPoints?.size.toString()
                tv_unknowp_total.text = it?.unknownPoints?.size.toString()
            }
        }

    }

    override fun getView(): View {
        return mView
    }

    override fun checkView(): Boolean {
        return true
    }

    override fun getTitle(): String {
        return "项目概述"
    }
}